package com.qms.repast.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.qms.repast.R;
import com.qms.repast.commons.GlideApp;
import com.qms.repast.entity.resbean.ShopDetailInfo.BodyBean.ShopRecommendListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 泅渡者
 * @date 2018/9/17 0017
 * @describe: 餐厅详情推荐适配器
 */
public class DiningAdapter extends BaseAdapter {

	private Context context;
	private List<ShopRecommendListBean> dishesInfoList;

	public DiningAdapter (Context context) {
		this.context = context;
		this.dishesInfoList = new ArrayList<>();
	}

	@Override
	public int getCount () {
		return dishesInfoList == null ? 0 : dishesInfoList.size();
	}

	@Override
	public Object getItem (int i) {
		return dishesInfoList == null ? null : dishesInfoList.get(i);
	}

	@Override
	public long getItemId (int i) {
		return i;
	}

	@Override
	public View getView (int i, View view, ViewGroup viewGroup) {
		ShopRecommendListBean dishesInfo = dishesInfoList.get(i);
		final ViewHolder holder;
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.layout_dining_gride_item, null);
			holder = new ViewHolder(view);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		GlideApp.with(context)
				.load(dishesInfo.getIcon())
				.placeholder(R.mipmap.default_pic)
				.apply(new RequestOptions()
							   .transforms(new CenterCrop(), new RoundedCorners(10)
							   ))
				.into(holder.ivDishesPic);
		holder.tvDishesName.setText(dishesInfo.getTitle());
		return view;
	}

	/**
	 * 增加数据
	 *
	 * @param dishesInfoList
	 */
	public void addData (List<ShopRecommendListBean> dishesInfoList) {
		if (null == dishesInfoList) {
			return;
		}
		for (ShopRecommendListBean shopRecommendListBean : dishesInfoList) {
			this.dishesInfoList.add(shopRecommendListBean);
		}
		notifyDataSetChanged();
	}

	/**
	 *获取当前点击数据对象
	 * @param position
	 */
	public ShopRecommendListBean getCurrentItem(int position){
		return	dishesInfoList.get(position);
	}
	class ViewHolder {
		@BindView(R.id.iv_dishesPic)
		ImageView ivDishesPic;
		@BindView(R.id.tv_dishesName)
		TextView tvDishesName;

		public ViewHolder (View view) {
			ButterKnife.bind(this, view);
		}
	}
}
